package edu.gdkm.dao;

import edu.gdkm.pojo.Company;
import org.apache.ibatis.annotations.*;
import java.util.List;

public interface CompanyDAO {
    // 增加公司信息
    @Insert("INSERT INTO company_info(user_id, company_name, industry, scale, nature, " +
            "introduction, address, website, contact_person, create_time, update_time) " +
            "VALUES(#{user.id}, #{companyName}, #{industry}, #{scale}, #{nature}, " +
            "#{introduction}, #{address}, #{website}, #{contactPerson}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Company company);

    // 删除公司信息
    @Delete("DELETE FROM company_info WHERE id = #{id}")
    int deleteById(Long id);

    // 更新公司信息
    @Update("UPDATE company_info SET user_id = #{user.id}, company_name = #{companyName}, " +
            "industry = #{industry}, scale = #{scale}, nature = #{nature}, " +
            "introduction = #{introduction}, address = #{address}, website = #{website}, " +
            "contact_person = #{contactPerson}, update_time = #{updateTime} WHERE id = #{id}")
    int update(Company company);

    // 查询所有公司信息
    @Select("SELECT id, user_id, company_name, industry, scale, nature, introduction, " +
            "address, website, contact_person, create_time, update_time FROM company_info")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "user", column = "user_id",
                    one = @One(select = "edu.gdkm.dao.UserDAO.selectById")),
            @Result(property = "companyName", column = "company_name"),
            @Result(property = "industry", column = "industry"),
            @Result(property = "scale", column = "scale"),
            @Result(property = "nature", column = "nature"),
            @Result(property = "introduction", column = "introduction"),
            @Result(property = "address", column = "address"),
            @Result(property = "website", column = "website"),
            @Result(property = "contactPerson", column = "contact_person"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<Company> selectAll();

    // 根据ID查询公司信息
    @Select("SELECT id, user_id, company_name, industry, scale, nature, introduction, " +
            "address, website, contact_person, create_time, update_time FROM company_info WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "user", column = "user_id",
                    one = @One(select = "edu.gdkm.dao.UserDAO.selectById")),
            @Result(property = "companyName", column = "company_name"),
            @Result(property = "industry", column = "industry"),
            @Result(property = "scale", column = "scale"),
            @Result(property = "nature", column = "nature"),
            @Result(property = "introduction", column = "introduction"),
            @Result(property = "address", column = "address"),
            @Result(property = "website", column = "website"),
            @Result(property = "contactPerson", column = "contact_person"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    Company selectById(Long id);

    // 根据用户ID查询公司信息
    @Select("SELECT id, user_id, company_name, industry, scale, nature, introduction, " +
            "address, website, contact_person, create_time, update_time FROM company_info WHERE user_id = #{userId}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "user", column = "user_id",
                    one = @One(select = "edu.gdkm.dao.UserDAO.selectById")),
            @Result(property = "companyName", column = "company_name"),
            @Result(property = "industry", column = "industry"),
            @Result(property = "scale", column = "scale"),
            @Result(property = "nature", column = "nature"),
            @Result(property = "introduction", column = "introduction"),
            @Result(property = "address", column = "address"),
            @Result(property = "website", column = "website"),
            @Result(property = "contactPerson", column = "contact_person"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    Company selectByUserId(Long userId);

    // 根据公司名称查询公司信息
    @Select("SELECT id, user_id, company_name, industry, scale, nature, introduction, " +
            "address, website, contact_person, create_time, update_time FROM company_info " +
            "WHERE company_name LIKE CONCAT('%', #{companyName}, '%')")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "user", column = "user_id",
                    one = @One(select = "edu.gdkm.dao.UserDAO.selectById")),
            @Result(property = "companyName", column = "company_name"),
            @Result(property = "industry", column = "industry"),
            @Result(property = "scale", column = "scale"),
            @Result(property = "nature", column = "nature"),
            @Result(property = "introduction", column = "introduction"),
            @Result(property = "address", column = "address"),
            @Result(property = "website", column = "website"),
            @Result(property = "contactPerson", column = "contact_person"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "updateTime", column = "update_time")
    })
    List<Company> selectByCompanyName(String companyName);
}
